package com.weizhong.xml;



import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;



import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlSerializer;



import com.weizhong.update.model.AppEntity;
import com.weizhong.update.util.Logger;



import android.nfc.Tag;
import android.util.Xml;



/**

 * 对xml文件进行解析

 * @author Administrator

 *

 */

public class PullParseService {

	

//	解析

	public static List<LocalConfigInfo> getInfos(InputStream inputStream) throws Exception{

		List<LocalConfigInfo> infos = null;

		LocalConfigInfo info = null;

		XmlPullParser parser = Xml.newPullParser();

		parser.setInput(inputStream, "UTF-8");

		

		int event = parser.getEventType();//产生第一个事件

		while(event!=XmlPullParser.END_DOCUMENT){

			switch(event){

			case XmlPullParser.START_DOCUMENT://判断当前事件是否是文档开始事件
				Logger.i("PullParseService", "XmlPullParser.START_DOCUMENT");

				infos = new ArrayList<LocalConfigInfo>();//初始化

				break;

			case XmlPullParser.START_TAG://判断当前事件是否是标签元素开始事件

				if("info".equals(parser.getName())){//判断开始标签元素是否是book

					info = new LocalConfigInfo();

				}

				if(info!=null){

					if("versionCode".equals(parser.getName())){//判断开始标签元素是否是name

						info.setVersionCode(parser.nextText());

					}else if("isDownSuccess".equals(parser.getName())){//判断开始标签元素是否是price

						info.setDownSuccess(Boolean.parseBoolean(parser.nextText()));

					}else if("downloadUrl".equals(parser.getName())){

						info.setDownloadUrl(parser.nextText());

					}else if("md5code".equals(parser.getName())){

						info.setMd5code(parser.nextText());

					}else if("fileName".equals(parser.getName())){

						info.setFileName(parser.nextText());

					}else if("updateInfo".equals(parser.getName())){

						info.setUpdateInfo(parser.nextText());

					}else if("fileSize".equals(parser.getName())){

						info.setFileSize(Long.parseLong(parser.nextText()));

					}

				}

				break;

			case XmlPullParser.END_TAG://判断当前事件是否是标签元素结束事件

				if("info".equals(parser.getName())){//判断结束标签元素是否是book

					infos.add(info);//将book添加到books集合

					info = null;

				}

				break;

			}

			event = parser.next();//进入下一个元素并触发相应事件

		}//end while

		Logger.i("pullparseService", "stream close pre");

			if(null!=inputStream){

				try {

					Logger.i("pullparseService", "stream close");

					inputStream.close();

				} catch (IOException e) {

					// TODO Auto-generated catch block

					e.printStackTrace();

				}

			}

		return infos;

	}
	
	/** 

     *  生成xml

     * @param persons 

     * @param outputStream fileoutputstream

     * @throws Exception 

     */  

    public static void buildXML(List<LocalConfigInfo> infos,OutputStream outputStream) throws Exception

    {

        XmlSerializer serializer=Xml.newSerializer();  

        serializer.setOutput(outputStream, "utf-8");  

          

        serializer.startDocument("utf-8", true);  

        serializer.startTag(null, "infos");  

          

        for(LocalConfigInfo info:infos)  

        {  

            serializer.startTag(null, "info");  

//            得到属性

//            serializer.attribute(null, "id", person.getId().toString());  

              

            serializer.startTag(null, "versionCode");  

            serializer.text(info.getVersionCode().toString());  

            serializer.endTag(null, "versionCode");  

              

            serializer.startTag(null, "isUpdateSuccess");  

            serializer.text(String.valueOf(info.isUpdateSuccess()));  

            serializer.endTag(null, "isUpdateSuccess");  

              

            serializer.startTag(null, "downloadUrl");  

            serializer.text(info.getDownloadUrl());  

            serializer.endTag(null, "downloadUrl"); 

            

            serializer.startTag(null, "fileName");  

            serializer.text(info.getFileName());  

            serializer.endTag(null, "fileName"); 

            

            serializer.startTag(null, "md5code");  

            serializer.text(info.getMd5code());  

            serializer.endTag(null, "md5code"); 

            

            serializer.startTag(null, "fileSize");  

            serializer.text(String.valueOf(info.getFileSize()));  

            serializer.endTag(null, "fileSize");             

            

            serializer.startTag(null, "updateInfo");  

            serializer.text(info.getUpdateInfo());  

            serializer.endTag(null, "updateInfo"); 

            

            serializer.endTag(null, "info");  

        }  

          

        serializer.endTag(null, "infos");  

        serializer.endDocument();  

        outputStream.flush();

        outputStream.close();  

    }
 
	public static List<AppEntity> getAppInfos(InputStream inputStream) throws Exception{

		List<AppEntity> infos = null;
		AppEntity info = null;

		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "UTF-8");

		int event = parser.getEventType();//产生第一个事件

		while(event!=XmlPullParser.END_DOCUMENT){
			switch(event){

			case XmlPullParser.START_DOCUMENT://判断当前事件是否是文档开始事件
				
				Logger.i("PullParseService", "XmlPullParser.START_DOCUMENT");
				infos = new ArrayList<AppEntity>();//初始化
				break;

			case XmlPullParser.START_TAG://判断当前事件是否是标签元素开始事件
				if("info".equals(parser.getName())){//判断开始标签元素是否是info
					info = new AppEntity();
				}

				if (info != null){
					if ("packageName".equals(parser.getName())){//判断开始标签元素是否是name
						info.setPackageName(parser.nextText());
					}else if("apkSize".equals(parser.getName())){//判断开始标签元素是否是price
						info.setApkSize(Long.parseLong(parser.nextText()));
					}else if("downloadUrl".equals(parser.getName())){
						info.setApkPath(parser.nextText());
					}else if("verCode".equals(parser.getName())){
						info.setVerCode(Long.parseLong(parser.nextText()));
					}else if ("action".equals(parser.getName())) {
						info.setAction(Integer.valueOf(parser.nextText()));
					}else if("isDeskTop".equals(parser.getName())){
						info.setIsDeskTop(Integer.parseInt(parser.nextText()));
					}else if("downloadSuccess".equals(parser.getName())){
						info.setDownloadSuccess(Boolean.parseBoolean(parser.nextText()));
					}else if("updateSuccess".equals(parser.getName())){
						info.setUpdateSuccess(Boolean.parseBoolean(parser.nextText()));
					}
				}
				break;

			case XmlPullParser.END_TAG://判断当前事件是否是标签元素结束事件
				if("info".equals(parser.getName())){//判断结束标签元素是否是info
					infos.add(info);//将info添加到infos集合
					info = null;
				}
				break;
			}

			event = parser.next();//进入下一个元素并触发相应事件
		}//end while

		Logger.i("pullparseService", "stream close pre");
		if(null!=inputStream){
			try {
				Logger.i("pullparseService", "app config xml stream close");
				inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return infos;
	}
    
    /**
     * 生成app 更新配置文件
     * @param appInfos
     * @param outputStream
     * @throws Exception
     */
    public static void buildAppXML(List<AppEntity> appInfos,OutputStream outputStream) throws Exception  {  

    	XmlSerializer serializer=Xml.newSerializer();  
        serializer.setOutput(outputStream, "utf-8");  

        serializer.startDocument("utf-8", true);  
        serializer.startTag(null, "infos");  

        for(AppEntity info:appInfos){  
            serializer.startTag(null, "info");

            serializer.startTag(null, "packageName");
            serializer.text(info.getPackageName().toString());
            serializer.endTag(null, "packageName");
            
            serializer.startTag(null, "apkSize");
            serializer.text(String.valueOf(info.getApkSize()));
            serializer.endTag(null, "apkSize");

            serializer.startTag(null, "downloadUrl");
            serializer.text(info.getApkPath());  
            serializer.endTag(null, "downloadUrl");

            serializer.startTag(null, "verCode");
            serializer.text(String.valueOf(info.getVerCode()));
            serializer.endTag(null, "verCode");
            
            serializer.startTag(null, "action");
            serializer.text(String.valueOf(info.getAction()));
            serializer.endTag(null, "action");
            
            serializer.startTag(null, "isDeskTop");
            serializer.text(String.valueOf(info.getIsDeskTop()));
            serializer.endTag(null, "isDeskTop");

            serializer.startTag(null, "downloadSuccess");
            serializer.text(String.valueOf(info.isDownloadSuccess()));
            serializer.endTag(null, "downloadSuccess");
            
            serializer.startTag(null, "updateSuccess");
            serializer.text(String.valueOf(info.isUpdateSuccess()));
            serializer.endTag(null, "updateSuccess");

            serializer.endTag(null, "info");
        }

        serializer.endTag(null, "infos");
        serializer.endDocument();
        outputStream.flush();
        outputStream.close();
    }
    
    /**

     * 更新xml

     * @param infos

     * @param outputStream

     * @return false 为未更新成功

     */

    public static boolean updateXml(List<LocalConfigInfo> infos,OutputStream outputStream){

    	

    	return  false;

    }

    

}  